"""
该模块主要用于验证模拟中的基本传播图像是否正确
"""
import matplotlib as mpl

mpl.use('Agg')
import sdf_helper as sh
import matplotlib.pyplot as plt
import numpy as np
import analyse as al
from analyse.sim_parm import *
# al.sp.c = 2.9e8
# #=============统计区间，参数========================================
# x = int(0 * micron / (xmax - xmin) * nx)  #选择x方向需要统计的起始点,微米为单位
# y = int(50 * micron / (xmax - xmin) * nx)  #选择x方向需要统计的起始点
# x = 3770 - 100 - 325
# y = 3770 + 100
# yx = int(0)  #选择y方向需要统计的起始点
# yy = int(ny)  #选择y方向需要统计的起始点
# xslice = slice(x, y)
# yslice = slice(yx, yy)
# al.sdf_prefix = '1'
# al.sdf_scope = [1]
#===============开始画图==============================================
fig = plt.figure(figsize=(22, 10))
for i in sdf_scope:
    print('loading data%d' % (i))
    data = sh.getdata('%s%.4d.sdf' % (sdf_prefix, i))
    var = data.Electric_Field_Ey
    print('loading ey')
    # density = data.Derived_Number_Density_electron
    print('build figure')
    X = var.grid_mid.data[0][xslice]
    Y = np.linspace(ymin, ymax, ny)
    X, Y = np.meshgrid(X, Y)
    Z = np.zeros([yy - yx, y - x])
    for j in range(yy - yx):
        for k in range(y - x):
            Z[j, k] = var.data[k + x]
    print(x)
    # print('X=========\n', var.data[x:x + 100])
    # print(Z[0:100, 0:100])
    ax = plt.subplot(1, 2, 1)
    tempax = al.plot.plot_2d(Z, X, Y, ax, ifsdf=0)
#==========以下进行数据分析
# ========================================================================================================
# find the maximum std
# Z = var.data.T  #Z为要进行操作的数据
# al.tool.data_analyse(Z, x, y, yx, yy)  #数据分析
# x_axis, y_axis = al.tool.find_nearest_value(Z, x, y, int(yy / 2), yy, 1e9)
# x_axis, y_axis = al.tool.cal_coordinate(x_axis, y_axis, 0, 0)
# # 尝试对上述结果x_axis,y_axis进行滤波
# #平均值滤波，删除明显偏移平均值的点,取出值为正的部分
# print('filter origin x:', len(x_axis), 'filter origin y:', len(y_axis))
# # x_axis, y_axis = filter_average(x_axis, y_axis)
# k = 0
# for i in range(len(x_axis)):
#     if (y_axis[k] < 0):
#         x_axis = np.delete(x_axis, k)
#         y_axis = np.delete(y_axis, k)
#     else:
#         k += 1
# print('deleted x_axis:', len(x_axis), 'deleted y_axis:', len(y_axis))
# #绘制极大值折线图
# ax.plot(x_axis, y_axis, label="simulation-max-value", color='b')

#===================================================
#=================绘制理论图二、在图一画出理论最大值曲线==========================================================================
#画出理论对比图,放在图二
# al.sp.c = 2.9e8
ax1 = plt.subplot(1, 2, 2)
ax1.set_title('theory_xy_profile t=%s' % (data.Header['time']))
# X, Y = np.meshgrid(var.grid_mid.data[0][xslice], var.grid_mid.data[1][yslice])
[rows, cols] = X.shape
Z = np.zeros([rows, cols], dtype=float)
for i in range(rows):
    for j in range(cols):
        # 无修正
        # Z[i, j] = laguerrel(50 * femto, X[i, j], Y[i, j], 0)
        # 修正
        Z[i, j] = al.mathfunc.plane_wave(1e-12, X[i, j], Y[i, j], 0)
# y = [laguerrel(i, 0.001, 0) for i in x]
al.plot.plot_2d(Z, X, Y, ax1, ifsdf=0)

#计算理论函数的包络线
# x_axis, y_axis = al.tool.find_nearest_value(Z, 0, y - x, int(yy / 2), yy, 1e9)
# x_axis, y_axis = al.tool.cal_coordinate(x_axis, y_axis, x, 0)
# #多项式拟合，做理论极大值曲线，并作图
# # #滤波+取正值
# # x_axis, y_axis = filter_average(x_axis, y_axis)
# # k = 0
# # for i in range(len(x_axis)):
# #     if (y_axis[k] < 0):
# #         x_axis = np.delete(x_axis, k)
# #         y_axis = np.delete(y_axis, k)
# #     else:
# #         k += 1
# #画图
# # print('x_axis==================================================')
# # print(x_axis)
# # print('y_axis==================================================')
# # print(y_axis)
# x_axis, y_axis = al.tool.filter_polyfit(x_axis, y_axis)
# ax.plot(x_axis, y_axis, label="theoretic-max-value", color='k')
ax.legend()

#前面三个子图的总宽度 为 全部宽度的 0.9；剩下的0.1用来放置colorbar
fig.subplots_adjust(right=0.9)

#colorbar 左 下 宽 高
l = 0.92
b = 0.12
w = 0.015
h = 1 - 2 * b

#对应 l,b,w,h；设置colorbar位置；
rect = [l, b, w, h]
cbar_ax = fig.add_axes(rect)
cb = plt.colorbar(tempax, cax=cbar_ax)

print('savefig')
plt.savefig('test')
plt.close()
#plt.show()
